﻿using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using log4net;

namespace FootBallMatchProject
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private static readonly ILog log = LogManager.GetLogger("Default Page");
       
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
               log.Info("Default Page::Page_Load" +  ex.Message);
            }
        }

        private static string GetCountryFlag()
        {
            try
            {
                string VisitorsIPAddr = string.Empty;
                //Users IP Address.                
                if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                {
                    //To get the IP address of the machine and not the proxy
                    VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
                {
                    VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
                }

                return VisitorsIPAddr;
            }
            catch (Exception ex)
            {
                log.Info("Default Page::GetCountryFlag()" + ex.Message);
                throw;
            }
        }

        [System.Web.Services.WebMethod]
        public static bool RegisterUser(string sUserFullName, string sUserEmailAddress)
        {

            SqlConnection objSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlClient"].ConnectionString);
            SqlCommand objSqlCommand = new SqlCommand();
            try
            {
                objSqlCommand.Connection = objSqlConnection;
                objSqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
                objSqlCommand.CommandText = "SP_INSERT_FIRST_BE_KNOW";
                objSqlCommand.Parameters.AddWithValue("@userFullName", sUserFullName);
                objSqlCommand.Parameters.AddWithValue("@userEamilAddress", sUserEmailAddress);
                objSqlCommand.Parameters.AddWithValue("@userRegion", GetCountryFlag());
                objSqlCommand.Parameters.AddWithValue("@dateTime_Reg", DateTime.Now.ToString());
                objSqlConnection.Open();
                int nReturnValue = objSqlCommand.ExecuteNonQuery();
                if (nReturnValue == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                log.Info("Default Page::RegisterUser()" + ex.Message);
                return false;
            }
            finally
            {
                objSqlConnection.Close();
            }
        }
    }
}